<?php
/**
 * @abstract All the resources have to be one word
 * 
 */
class User_WidgetController extends Clu_Controller_Action_Widget
{
	public function init()
	{
		//require_once (APP_PATH.'/modules/comment/Model/Comment.php');
		//require_once (APP_PATH.'/modules/user/Model/User.php');
		$this->mComment = new comment_Model_Service();
		$this->m = new user_Model_User();
		parent::init();
	}
	
    public function loginAction()
    {
    	//require_once (APP_PATH.'/modules/user/Form/Login.php');
    	$this->viewParams['form'] = new user_Form_Login();
        $this->viewParams['form']->cluBuild();
    }
    
    public function homeAction()
    {
    	
    }
    
    public function viewProfileAction()    
    {
    	//get 'u' user information
		$this->viewParams['u'] = $this->getParam('u');
    }
    
    public function profileTabMenuAction()
    {
    	//$this->viewParams['id'] = $this->getParam('id');
    	if(isset($_REQUEST['id'])){
    	  $this->viewParams['id'] = $_REQUEST['id'];
    	}
    	$result = $this->m->getUserRoles($_REQUEST['id']);
    	$ret = explode(',', $result);
    	$this->viewParams['role_id'] = $ret;
    	//$this->viewParams['type'] = $this->getParam('type');
    	//$this->viewParams['currentTab'] = $this->getParam('currentTab');
    }
    
    public function mailMenuAction()
    {
    	$receiverId = $this->getParam('viewer_id');
    	//number of unread mails
		$sql = "SELECT count(*) AS count FROM `private_messages` WHERE status=0 AND receiver_id=$receiverId";
		$db = Zend_Db_Table::getDefaultAdapter() ;
		$ret = $db->fetchRow($sql);
		if ($ret['count'] > 0)
			$this->viewParams['unread'] = $ret['count'];		
		$this->viewParams['viewer_id'] = $receiverId;
    }
    
    //generate info for :avatar, friends, recent activities => side bar
    public function viewUserInfoAction()
    {
    	
    	$id = $_REQUEST['id'];
    	$sql = "SELECT * from user_followship WHERE uid1 = ".$id;
		$this->viewParams['friend'] = $this->db->fetchAll($sql);
    	
    	if (file_exists(APP_PATH."/public/ufiles/$id/images/original/avatar/avatar.jpg"))
		{
			$this->viewParams['avatar_image'] = "/public/ufiles/$id/images/%s/avatar/avatar.jpg";
		}
		else
			$this->viewParams['avatar_image'] = "";
		
    	if (!$u = $this->m->getUserData($id))
        {
            $this->renderError(null,t(406));
            return;
        }
    	
    	 $this->viewParams['u'] = $u;
    }
    
    //generate info for user's public messages (comments)
    public function viewUserPublicMessageAction()
    {
    	if(isset($_REQUEST['id'])){
    	$id = $_REQUEST['id'];
		$temp['content_id'] = $id; 
		$temp['content_type'] = 'user';
		$temp['asc_or_desc'] = 'DESC';
		$temp['orderBy'] = 'time'; 
		
		$sql = $this->mComment->search($temp);
		
		$this->viewParams['comments'] = $this->db->fetchAll($sql);
		
		$sess = new Zend_Session_Namespace(CLU_NS);
		$this->viewParams['form'] = new comment_Form_Comment();
		
		$actionUrl = "/" .$sess->viewLang . "/comment/ajax/new-comment";
        $this->viewParams['form']->cluBuild(array('action' => 'new', 
        						'action_url' => $actionUrl, 
        						'control' => 'ajax', 'content_type' =>'user',
                                'userPermissions' => $sess->userPermissions,
        						'region' => $this->widget['region'], 
                                'language' => $sess->viewLang ));
        
        $this->viewParams['content_id'] = $id;
        $this->viewParams['content_type'] = 'user';
        $this->viewParams['region'] = $this->widget['region'];
    	}
    }
    
    public function recentVisitorAction()
    {
    	if($this->issetParam('visitorId'))
    	{
    		$userId = $this->getParam('visitorId');
    		$this->viewParams['visitor_user'] =$this->m->getVisitorUser($userId, true);
    	} 
    }
    
}    
